"use strict";

const CACHE_PATH = 'cache'
const SQL_PATH = CACHE_PATH + '/AT.sqlite3';

const fs = require("fs");

if(!fs.existsSync(CACHE_PATH)) fs.mkdirSync(CACHE_PATH);

const sqlite3 = require('sqlite3').verbose();
const DB = new sqlite3.Database(SQL_PATH, createTables);
var created = false;

function createTables() {
    // if(fs.existsSync(SQL_PATH)) return;
    if(created) return;
    DB.parallelize(() => {
        DB.run("CREATE TABLE IF NOT EXISTS mouse_pos (id INTEGER PRIMARY KEY AUTOINCREMENT,pos TEXT,note TEXT, c_time INT);");
        DB.run("CREATE TABLE IF NOT EXISTS trigger_script (id INTEGER PRIMARY KEY AUTOINCREMENT,script TEXT,note TEXT DEFAULT '', c_time INT);");
        DB.run("CREATE TABLE IF NOT EXISTS trigger_log (s_id INT,p_id INT,t_price FLOAT, t_time INT);");
        DB.run("CREATE TABLE IF NOT EXISTS price_log (price FLOAT, c_time INT)");
        created = true;
    });
}

module.exports.DB = DB;